<?php 
class integralChangeModel extends CDBModelBase {
	public function getTabelName(){
		return "t_integral_change";
	}

	public function getPKID(){
		return "c_id";
	}
	
	protected function getSequence()
	{
		return "CHANGE_SEQUENCE";
	}
	
	protected $_dateColumns="cost_time";
	
	protected $_moneyColumns = array("discount_num","cost_money","cost_before_money","cost_after_money","cost_shop_before_money","cost_shop_after_money","cost_money_t","discount","discount_num","c_discount");
	
	public function getReport($where)
	{
		$sql = "select 
				cost_type, sum(cost_money_t) as cost_money,
 				sum(decode(is_cancel,1,cost_money_t,0)) as cost_money_cancel,
 				sum(decode(is_cancel,0,cost_money_t,0)) as cost_money_sj 
				from ".$this->getTabelName()." ic, t_owner t where ic.owner_id=t.owner_id and ".$where." group by shop_id,cost_type";

		$data = $this->db->executeDataSet($sql);
		
		$cost_money_add = 0;
		$cost_money_reduce = 0;
		$cost_money_add_cancel = 0;
		$cost_money_reduce_cancel = 0;
		$cost_money_add_sj = 0;
		$cost_money_reduce_sj = 0;
		
		foreach($data as $item)
		{
			if($item["cost_type"]==1)
			{
				$cost_money_add = $item["cost_money"];
				$cost_money_add_cancel = $item["cost_money_cancel"];
				$cost_money_add_sj = $item["cost_money_sj"];
			}
			else if($item["cost_type"]==2)
			{
				$cost_money_reduce = $item["cost_money"];
				$cost_money_reduce_cancel = $item["cost_money_cancel"];
				$cost_money_reduce_sj = $item["cost_money_sj"];
			}
		}
		
		return array("cost_money_add"=>$cost_money_add,"cost_money_reduce"=>$cost_money_reduce,
				"cost_money_add_cancel"=>$cost_money_add_cancel,"cost_money_reduce_cancel"=>$cost_money_reduce_cancel,
				"cost_money_add_sj"=>$cost_money_add_sj,"cost_money_reduce_sj"=>$cost_money_reduce_sj);
	}
	
	/**
	 * 商家积分统计-管理员后台
	 * @param unknown_type $shopID
	 * @param unknown_type $costType
	 */
	public function getTjData($where,$type=1)
	{
		if($type==3)
		{
			$sql = "select sum(decode(cancel_cost_type,1,cost_money_t,0)) as cancel_money_add,
				sum(decode(cancel_cost_type,2,cost_money_t,0)) as cancel_money_reduce 
				from ".$this->getTabelName()." where ".$where." group by shop_id";
			
			$r = $this->db->executeFetch($sql);
			
			if(!$r)
			{
				$r["cancel_money_add"] = 0;
				$r["cancel_money_reduce"] = 0;
			}
			
			return $r;
		}
		
		$sql = "select ";
		if($type==1)
		{
			$sql .= "sum(cost_money) ";
		}
		else{
			$sql .= "sum(cost_money_t) ";
		}
		
 		$sql .= "as cost_money,sum(decode(discount_type,1,discount_num,0)) as add_sum,
 			sum(decode(is_cancel,1,cost_money_t,0)) as cost_money_cancel,
 			sum(decode(is_cancel,0,cost_money_t,0)) as cost_money_sj 
			from ".$this->getTabelName()." where ".$where." group by shop_id";

		$r = $this->db->executeFetch($sql);
		
		if(!$r)
		{
			$r["cost_money"] = 0;
			$r["add_sum"] = 0;
			$r["cost_money_cancel"] = 0;
			$r["cost_money_sj"] = 0;
		}
		
		return $r;
	}
}
?>
